package org.zhadoop.streaming.event;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 事件类型管理
 * @author zwd
 *
 */
public class EventTypeMng implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2018243091331553845L;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(EventTypeMng.class);
	
	/**
	 * MAP<数据类型名称，具体数据类型>
	 */
	private Map<String, IEventType> schemas;
	
	public EventTypeMng() {
		this.schemas = new HashMap<String,IEventType>();
	}
	
	public void addEventType(IEventType schema){
		if(null == schema){
			throw new RuntimeException("The event type is null.");
		}
		
		if(StringUtils.isEmpty(schema.getEventTypeName())){
			throw new RuntimeException("The event type name is empty.");
		}
		
		LOGGER.info("AddEventType enter, the eventtypeName is:{}.", schema.getEventTypeName());
		
		if(schemas.containsKey(schema.getEventTypeName())){
			throw new RuntimeException("The event type name: " + schema.getEventTypeName() + " is already exist.");
		}
		
		schemas.put(schema.getEventTypeName(),schema);
	}
	
	
	/**
	 * 根据事件类型名称获得具体事件类型
	 * @param name
	 * @return
	 */
	public IEventType getEventType(String name){
		if(StringUtils.isEmpty(name)){
			LOGGER.error("The schema name is null.");
			return null;
		}
		
		if(!schemas.containsKey(name)){
			LOGGER.error("The schema name {} is not exist.", name);
            return null;
		}
		return schemas.get(name);
	}
	
	
}
